package com.bjsxt.usersCenter.outer.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.usersCenter.common.util.ConstatFinalUtil;
import com.bjsxt.usersCenter.common.util.DateFormatUtil;
import com.bjsxt.usersCenter.outer.service.IOuterService;
import com.bjsxt.usersCenter.system.pojo.ARegion;
import com.bjsxt.usersCenter.system.service.ISystemService;
@Service("outerService")
public class IOuterServiceImpl implements IOuterService
{
	
	@Resource
	private ISystemService systemService;

	/**
	 *验证 token是否有效，第一个版本
	 *reqJSON 上行的JSON字符串
	 *直接返回下行的字符串
	 */
	@Override
	public JSONObject validatorAdmins01Service(JSONObject reqJSON)
	{
		DateFormatUtil dateFormatUtil=new DateFormatUtil();
		JSONObject dataReqJSON =reqJSON.getJSONObject("data");
		JSONObject resultJSON=new JSONObject();
		/*下行的data中的json*/
		JSONObject dataResJSON=new JSONObject();
		/*从上行的信息中取得token*/
		String token =dataReqJSON.getString("token")+"";
		/*从token中获取管理员的json信息*/
		JSONObject adminsJSON=ConstatFinalUtil.TOKENS_MAP.get(token);
		if(adminsJSON!=null)
		{
			/*存储当前时间*/
			adminsJSON.put("validate", dateFormatUtil.format(new Date()));
			dataResJSON.put("admins", adminsJSON);
			
			resultJSON.put("data", dataResJSON);
			resultJSON.put("version", reqJSON.getString("version"));
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
		}else{
			resultJSON.put("code", "10");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("10"));
			
			
		}
		return resultJSON;
	}

	@Override
	public JSONObject regionList01Service(JSONObject reqJSON)
	{
		
		JSONObject resultJSON=new JSONObject();
		
		JSONObject dataReqJSON=reqJSON.getJSONObject("data");
		
		String parentId=dataReqJSON.getString("parentId");
		
		Map<String,Object> condMap=new HashMap<String, Object>();
		
		condMap.put("parentId", parentId);
		
		
		List<ARegion> childRegionList=this.systemService.findCondListRegionService(null, condMap);
		JSONArray regionList=new JSONArray();
		
		for (ARegion region : childRegionList)
		{
			JSONObject regionChildJSON=region.toJSON();
		
			regionList.add(regionChildJSON);
		
		}
		
		JSONObject dataResJSON=new JSONObject();
		dataResJSON.put("regionList", regionList);
		
		resultJSON.put("code", "0");
		resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
		resultJSON.put("data", dataResJSON);
		return resultJSON;
	}
	
	
	
	
}
